package cosc617_blackteam;

import java.util.ArrayList;


/**
 * @author Laura
 * tries pushing together empty tables to see if it results in one large
 * enough to seat the party. then creates a new table of the party size
 * and leaves any left over tables as a separate table. does not push together
 * tables if they won't seat the party
 */
public class PushTablesTogetherStrategy implements I_SeatingStrategy {
	private TableCollections tableList;
	public PushTablesTogetherStrategy(TableCollections tableList){
		this.tableList = tableList;
	}
	@Override
	public boolean addParty(CustomerParty party) {
		//this array is for the tables we are combining and need to delete
		ArrayList<Table> emptyTableArray = new ArrayList<Table>();
		int seatingCapacity = 0;
	
		for(Table table : tableList){
			//if there is a table that fits without pushing tables together seat party
			if(table.addCustomerParty(party)){
				return true;
			}
			else if (table.isEmpty()){
			//otherwise push enough empty tables together until you can seat the party
			seatingCapacity += table.getTotalSeatingSize();
			emptyTableArray.add(table);
			if (party.getSize() <= seatingCapacity){
				for(Table t : emptyTableArray){
					tableList.removeTable(t);
					
				}
				//take the tables that are pushed together and make one big enough to seat the party
				//and the remainder a new table
				
				Table newTable = new Table(party.getSize(), 0);
				newTable.addCustomerParty(party);
				tableList.addTable(newTable);
				//if there are remaining empty seats make a new table out of them
				if(seatingCapacity - party.getSize() > 0){
					newTable = new Table(seatingCapacity - party.getSize(), 0);
					tableList.addTable(newTable);	
				}
				return true;
			}
		}	
	}
		
		return false;
	}

}
